<?php

require_once 'mp3\Smarty.php';
require_once 'mp3\Constants.php';

$smarty = new mp3_Smarty();

require_once 'mp3\GestionarUsuari.php';

require 'mp3/ComprovarSessio.php';

if (array_key_exists('usuari', $_POST)){
	$login = new mp3_GestionarUsuari($_POST['usuari'],$_POST['pdp']);
	$login->login();
	if ($login->ok()){
		//TODO: crear constantes con los nombres de las cookies
		setcookie('userId', $login->getUserId(), time()+3600);
		setcookie('userIdCrypt', sha1(Mp3_Constants::SALT.$login->getUserId()), time()+3600);
		setcookie('userName', $login->getUserName(), time()+3600);
		$_SESSION['userid'] = $login->getUserId();
		$_SESSION['username'] = $login->getUserName();
		require 'mp3/ComprovarSessio.php';
		echo $smarty->fetch('mp3\header.tpl');
		echo $smarty->fetch('mp3\index.tpl');
		echo $smarty->fetch('mp3\footer.tpl');
	}else{
		$smarty->assign('missatge', $login->getError());
		echo $smarty->fetch('mp3\header.tpl');
		echo $smarty->fetch('mp3\index.tpl');
		echo $smarty->fetch('mp3\footer.tpl');
	}
}else{
	$smarty->assign('missatge', '');
	echo $smarty->fetch('mp3\header.tpl');
	echo $smarty->fetch('mp3\index.tpl');
	echo $smarty->fetch('mp3\footer.tpl');
}

?>